﻿using System;
using Besture.Common;
using Besture.Common.Helper;

namespace Besture.UI.Admin.Control
{
    public partial class UploadSingleFile : System.Web.UI.UserControl
    {
        /// <summary>
        /// DIV ID
        /// </summary>
        public string FileDivID = Guid.NewGuid().ToString();
        /// <summary>
        /// 上传文件路径
        /// </summary>
        public string FolderPath { get; set; }
        /// <summary>
        /// 是否必须上传
        /// </summary>
        public bool Required { get; set; }
        /// <summary>
        /// 必须上传文件
        /// </summary>
        public string RequiredFileUrl = "/Admin/Themes/Images/dispensable.png";
        /// <summary>
        /// 文件上传限制
        /// </summary>
        public string FileTypeExts { get; set; }
        /// <summary>
        /// 文件上传限制说明
        /// </summary>
        public string FileTypeDesc { get; set; }
        /// <summary>
        /// 提示文字
        /// </summary>
        public string Tips { get; set; }
        /// <summary>
        /// 文件名称
        /// </summary>
        private string FileName { get; set; }
        /// <summary>
        /// 文件地址
        /// </summary>
        private String FinalFileUrl { get; set; }
        /// <summary>
        /// 上传文件大小
        /// </summary>
        public string FileSize
        {
            get { return string.IsNullOrEmpty(hfFileSize.Value) ? "0MB" : System.Math.Round(Convert.ToDouble((double)Convert.ToInt32(hfFileSize.Value) / (double)1048576), 2).ToString() + "MB"; }
            set { hfFileSize.Value = value; }
        }
        /// <summary>
        /// 文件地址
        /// </summary>
        public string FileUrl
        {
            get { return txtFileUrl.Text; }
            set
            {
                //设置示例 “2014-03-18/201403181444260591.rar|测试.rar”
                var str = value;
                if (!string.IsNullOrEmpty(str))
                {
                    string[] args = StringHelper.SplitStringByChar(str, 1);
                    if (args.Length == 2)
                    {
                        FileName = args[1];
                        FinalFileUrl = args[0];
                    }
                }
                txtFileUrl.Text = value;
            }
        }

        /// <summary>
        /// 临时文件文件夹
        /// </summary>
        public string folderPathTemp;

        public string InitText = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {

            txtFileUrl.Text = FileUrl;
            //设置缺省上传文件路径 
            if (string.IsNullOrEmpty(FolderPath))
            {
                FolderPath = Constants.PATH_FILE_CONTENT;
            }
            //必须上传
            if (Required)
            {
                txtFileUrl.CssClass = "FileRequired";
                RequiredFileUrl = "/Admin/Themes/Images/indispensable.gif";
            }

            InitText = InitPhotoDiv();
        }


        /// <summary>
        /// 初始化文件HTML
        /// </summary>
        /// <returns></returns>
        protected string InitPhotoDiv()
        {
            string text = string.Empty;
            if (!string.IsNullOrEmpty(FileUrl) && FileUrl != Constants.PATH_FILE_CONTENT)
            {
                text = text + ("<div class='element' id='" + FileDivID + "_div'>");
                text = text + ("    <div>");
                text = text + ("         已上传文件：【<a href='" + FolderPath + FinalFileUrl + "'>" + FileName + "</a>】,右键另存可下载！");
                text = text + ("    </div>");
                text = text + ("</div>");
            }
            return text;
        }
    }
}